<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" v-loading="loading" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item prop="code" :label="$t('dept.code')">
        <el-input v-model="dataForm.code" :placeholder="$t('dept.code')"></el-input>
      </el-form-item>
      <el-form-item prop="name" :label="$t('dept.name')">
        <el-input v-model="dataForm.name" :placeholder="$t('dept.name')"></el-input>
      </el-form-item>
      <el-form-item prop="pid" :label="$t('dept.parentName')">
        <uni-tree-select v-model="dataForm.pid" ref="treeSelectDept" :data="deptList" :selectParams="{placeholder:$t('dept.parentNameDefault')}" />
      </el-form-item>
      <el-form-item prop="deptType" :label="$t('dept.deptType')">
        <el-select v-model="dataForm.deptType" :placeholder="$t('dept.deptType')" clearable style="width: 100%">
          <el-option v-for="template in $getDictList('SYS_DEPT_TYPE', true)" :key="template.val" :label="template.label" :value="template.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="sort" :label="$t('dept.sort')">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" :label="$t('dept.sort')">
        </el-input-number>
      </el-form-item>
      <el-form-item prop="status" :label="$t('dept.state')" size="mini">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">{{ $t('dept.status1') }}</el-radio>
          <el-radio :label="0">{{ $t('dept.status0') }}</el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      deptList: [],
      deptListVisible: false,
      dataForm: {
        id: '',
        code: '',
        name: '',
        pid: '',
        parentName: '',
        sort: 0,
        status: 1,
        deptType: ''
      },
      pids: {}
    }
  },
  watch: {
    'dataForm.pid': function (val) {
      if (!val) {
        this.deptListTreeSetDefaultHandle()
      }
    }
  },
  components: {},
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        parentName: [
          { required: true, message: this.$t('validate.required'), trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm = {
          ...this.dataForm,
          ...this.initParams
        }
        if (this.dataForm.id) {
          this.getInfo()
        } else {
          if (!this.dataForm.pid) {
            this.deptListTreeSetDefaultHandle()
          }
          this.getSortInfo()
        }
      })
    },
    // 获取最大排序
    getSortInfo () {
      this.$http.get(`/sys/dept/getDeptSort`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.dataForm.sort = res.data.sort
      }).catch(() => { })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/sys/dept/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.pids.old = this.dataForm.pid
        if (this.dataForm.pid === '0') {
          return this.deptListTreeSetDefaultHandle()
        }
        this.$refs.deptListTree.setCurrentKey(this.dataForm.pid)
      }).catch(() => { })
    },
    // 上级部门树清空, 设置默认值
    deptListTreeSetDefaultHandle () {
      this.dataForm.pid = '0'
      // this.dataForm.parentName = this.$t('dept.parentNameDefault')
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/dept', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.pids.new = this.dataForm.pid
              this.$emit('refreshDataList', this.pids)
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
